package sk.vasicek.drinkagent.activity;

import java.util.ArrayList;
import java.util.HashMap;

import sk.vasicek.drinkagent.R;
import sk.vasicek.drinkagent.domain.DrinkEntry;
import sk.vasicek.drinkagent.service.BackgroundAdapter;
import sk.vasicek.drinkagent.service.DrinkHistoryHelper;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.Toast;

public class DrinkHistoryActivity extends Activity {

	private static String TAG = "DrinkPlacesActivity";

	protected ListView drinkEntryList;
	protected ArrayAdapter<String> adapter;
	protected ArrayList<String> drinkEntrysList;
	protected HashMap<String, DrinkEntry> drinkEntrysMap;

	protected ImageButton settingsButton;
	protected ImageButton mapButton;
	protected ImageButton placesButton;
	protected ImageButton drinksButton;

	protected DrinkHistoryHelper drinkDatabaseHelper;
	SharedPreferences prefs;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.drink_places);
		Log.d(TAG, "Started DrinkPlacesActivity");
		prefs = PreferenceManager.getDefaultSharedPreferences(this);
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);

		updateDrinkEntrys();
		this.drinkEntryList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int no, long arg3) {
				// Toast toast = Toast.makeText(getApplicationContext(),
				// "You picked " + contactPhoneList.get(no),
				// Toast.LENGTH_SHORT);
				// toast.show();
				Intent intent = new Intent(DrinkHistoryActivity.this, DrinkHistoryDetailActivity.class);
				DrinkEntry drinkEntry = drinkEntrysMap.get(drinkEntrysList.get(no));
				if (drinkEntry.getId() != null) {
					intent.putExtra(DrinkEntry.DRINK_ID, drinkEntry.getId());
				}
				if (drinkEntry.getName() != null && !"".equals(drinkEntry.getName())) {
					intent.putExtra(DrinkEntry.DRINK_NAME, drinkEntry.getName());
				}
				if (drinkEntry.getNote() != null && !"".equals(drinkEntry.getNote())) {
					intent.putExtra(DrinkEntry.DRINK_NOTE, drinkEntry.getNote());
				}
				if (drinkEntry.getVolume() != 0d) {
					intent.putExtra(DrinkEntry.VOLUME, drinkEntry.getVolume());
				}
				if (drinkEntry.getConcentration() != 0d) {
					intent.putExtra(DrinkEntry.ALCOHOL_CONCENTRATION, drinkEntry.getConcentration());
				}
				startActivity(intent);
			}

		});

		this.mapButton = (ImageButton) this.findViewById(R.id.map);
		mapButton.setVisibility(View.VISIBLE);
		this.mapButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkMapsActivity.class);
				startActivity(intent);
			}
		});
		this.placesButton = (ImageButton) this.findViewById(R.id.places);
		placesButton.setVisibility(View.VISIBLE);
		this.placesButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

				Intent intent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
				startActivity(intent);
			}
		});

		this.settingsButton = (ImageButton) this.findViewById(R.id.settings);
		settingsButton.setVisibility(View.VISIBLE);
		this.settingsButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
				startActivity(intent);
			}
		});

		this.drinksButton = (ImageButton) this.findViewById(R.id.drinks);

		this.drinksButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
				startActivity(intent);
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();

			}
		});

	}

	private void updateDrinkEntrys() {
		drinkEntrysList = new ArrayList<String>();
		drinkEntrysMap = new HashMap<String, DrinkEntry>();

		ArrayList<DrinkEntry> drinkEntrys = new ArrayList<DrinkEntry>();

		drinkDatabaseHelper = new DrinkHistoryHelper(this);
		if (drinkDatabaseHelper != null) {
			try {
				drinkEntrys = drinkDatabaseHelper.getAllDrinksFromHistory();
			} catch (Exception ex) {
				Log.e(TAG, "Failed to read drink points", ex);
			}
			for (DrinkEntry drinkEntry : drinkEntrys) {
				String key = drinkEntry.getName() + ": " + drinkEntry.getPrice() + getResources().getText(R.string.priceCurrency) + " @ " + drinkEntry.getTimeDrinked().getHours() + ":"
						+ drinkEntry.getTimeDrinked().getMinutes() + ":" + drinkEntry.getTimeDrinked().getSeconds();

				drinkEntrysMap.put(drinkEntry.getId() + ": " + key, drinkEntry);
				drinkEntrysList.add(drinkEntry.getId() + ": " + key);
			}
		}
		this.drinkEntryList = (ListView) findViewById(R.id.drinkPlaceList);
		adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.drink_list_item, drinkEntrysList);
		drinkEntryList.setAdapter(adapter);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.optionmenu, menu);
		MenuItem settings = menu.findItem(R.id.settings);
		settings.setIntent(new Intent(this, SettingsActivity.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			DrinkHistoryActivity.this.finish();
			return true;
		case R.id.settings:
			Intent settingsIntent = item.getIntent();
			this.startActivityForResult(settingsIntent, 0);
			return true;
		case android.R.id.home:
			// Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(this, DrinkManagerListActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent);
			return true;
		case R.id.drinks:
			Intent drinksIntent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
			this.startActivityForResult(drinksIntent, 0);
			return true;
		case R.id.places:
			// Toast.makeText(getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();
			Intent placesIntent = new Intent(this, DrinkPlacesActivity.class);
			this.startActivityForResult(placesIntent, 0);
			return true;
		case R.id.custom:
			Intent customIntent = new Intent(this, CustomDrinksActivity.class);
			this.startActivityForResult(customIntent, 0);
			return true;
		case R.id.history:
			// Intent historyIntent = new Intent(this,
			// DrinkHistoryActivity.class);
			// this.startActivityForResult(historyIntent, 0);
			Toast.makeText(getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case R.id.graphs:
			Intent graphsIntent = new Intent(this, DrinkGraphActivity.class);
			this.startActivityForResult(graphsIntent, 0);
			return true;
		case R.id.back:
			Intent backIntent = new Intent(getApplicationContext(), BackgroundChooserActivity.class);
			this.startActivityForResult(backIntent, 0);
			return true;
		case R.id.map:
			Intent mapsIntent = new Intent(getApplicationContext(), DrinkMapsActivity.class);
			this.startActivityForResult(mapsIntent, 0);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	@Override
	protected void onResume() {
		updateDrinkEntrys();
		super.onResume();
		BackgroundAdapter.updateBackgroundFromPrefs(prefs, this);
	}
}
